#include "mytcpserver.h"
#include<QDebug>
#include"tcpserver.h"
#include<QThread>
MyTcpServer::MyTcpServer(QObject *parent)
    : QTcpServer{parent}
{
    m_dbm=DBManager::getInstance();
}

MyTcpServer::~MyTcpServer()
{
    qDebug()<<"MyTcpServer类 析构函数";
    DBManager::release();
}

void MyTcpServer::incomingConnection(qintptr handle)
{
    m_tcpId++;
    displayLog("@@系统@@ :服务器加入新的客户端tcp连接："+ QString::number(handle));
    MyTcpSocket *tcpSocket=new MyTcpSocket;
    tcpSocket->setSocketDescriptor(handle);         //与对应客户端进行绑定

    QThread *thread = new QThread;
    tcpSocket->moveToThread(thread);
    thread->start();
    tcpSocket->setTcpId(m_tcpId);

    m_tcpSocketList.append(tcpSocket);

    connect(tcpSocket,&MyTcpSocket::logSignal,this,&MyTcpServer::displayLog);
    connect(tcpSocket,&MyTcpSocket::forwardMsg,this,&MyTcpServer::forwardMsg);
    connect(this,&MyTcpServer::sendDataToSocket,tcpSocket,&MyTcpSocket::sendData);
    connect(tcpSocket, &MyTcpSocket::offline, this,&MyTcpServer::deleteSocket);

    //以下为数据库相关操作
    //1.注册
    connect(tcpSocket,&MyTcpSocket::registSignal,m_dbm,&DBManager::handleRegist);
    connect(m_dbm,&DBManager::registResult,tcpSocket,&MyTcpSocket::handleRegistRequest);
    //2.登录
    connect(tcpSocket,&MyTcpSocket::loginSignal,m_dbm,&DBManager::handleLogIn);
    connect(m_dbm,&DBManager::loginResult,tcpSocket,&MyTcpSocket::handleLoginRequest);
    //上线通知转发
    connect(tcpSocket,&MyTcpSocket::onlineSignalForward,m_dbm,&DBManager::handleOnlineForward);
    connect(m_dbm,&DBManager::onlineForwardFriends,tcpSocket,&MyTcpSocket::handleOnlineForwardFriendsRequest);

    //下线通知转发offlineSignalForward
    connect(tcpSocket,&MyTcpSocket::offlineSignalForward,m_dbm,&DBManager::handleOfflineForward);
    connect(m_dbm,&DBManager::offlineForwardFriends,tcpSocket,&MyTcpSocket::handleOfflineForwardFriendsRequest);

    //12.注销
    connect(tcpSocket,&MyTcpSocket::logoffSignal,m_dbm,&DBManager::handleLogOff);
    connect(m_dbm,&DBManager::logoffResult,tcpSocket,&MyTcpSocket::handleLogoffRequest);
    //修改用户信息
    connect(tcpSocket,&MyTcpSocket::modifyUsrInfoSignal,m_dbm,&DBManager::handleModifyUsrInfo);
    connect(m_dbm,&DBManager::usrModifyInfoResult,tcpSocket,&MyTcpSocket::handleModifyInfoRequest);

    //3.下线
    connect(tcpSocket,&MyTcpSocket::usrOfflineSignal,m_dbm,&DBManager::handleOffline);
    connect(m_dbm,&DBManager::usrOfflineResult,tcpSocket,&MyTcpSocket::handleUsrOfflineRequest);

    //4.查询所有在线用户
    connect(tcpSocket,&MyTcpSocket::searchOnlineUsrsSignal,m_dbm,&DBManager::handleSearchAllOnlineUsrs);
    connect(m_dbm,&DBManager::onlineUsrsResult,tcpSocket,&MyTcpSocket::handleOnlineUsersRequest);

    //-查询用户所有在线好友
    connect(tcpSocket,&MyTcpSocket::searchOnlineFriendsSignal,m_dbm,&DBManager::handleSearchAllOnlineFriends);
    connect(m_dbm,&DBManager::onlineFriendsResult,tcpSocket,&MyTcpSocket::handleGetOnlineFriRequest);

    //5.根据名称查找用户
    connect(tcpSocket,&MyTcpSocket::searchUsrByNameSignal,m_dbm,&DBManager::handleSearchUsr);
    connect(m_dbm,&DBManager::searchResultReady,tcpSocket,&MyTcpSocket::handleSearchResult);

    //6.添加好友
    connect(tcpSocket,&MyTcpSocket::addFriendSignal,m_dbm,&DBManager::handleAddFriend);
    connect(m_dbm,&DBManager::addFriendResult,tcpSocket,&MyTcpSocket::handleAddFriendRequest);

    //7.同意添加好友
    connect(tcpSocket,&MyTcpSocket::agreeAddFriendSignal,m_dbm,&DBManager::handleAddFriendAgree);
    //8.不同意添加好友
    //9.刷新好友列表
    connect(tcpSocket,&MyTcpSocket::flushFriendSignal,m_dbm,&DBManager::handleFlushFriend);
    connect(m_dbm,&DBManager::flushFriendRequest,tcpSocket,&MyTcpSocket::handleFlushFriendRequest);
    //10.删除好友
    connect(tcpSocket,&MyTcpSocket::deleteFriendSignal,m_dbm,&DBManager::handleDelFriend);
    connect(m_dbm,&DBManager::delFriendRequest,tcpSocket,&MyTcpSocket::handleDeleteFriendRequest);
    //11.好友群聊
    connect(tcpSocket,&MyTcpSocket::groupChatSignal,m_dbm,&DBManager::handleGroupChatFriendList);
    connect(m_dbm,&DBManager::groupChatRequest,tcpSocket,&MyTcpSocket::handleGroupChatRequest);
}

int MyTcpServer::getTcpNums()
{
    return m_tcpSocketList.size();
}

//转发消息
void MyTcpServer::forwardMsg(const QString& desName,PDU *pdu)
{
    //desName 目标用户名
    if(desName==NULL||pdu==NULL){
        return;
    }
    for (MyTcpSocket *sock : m_tcpSocketList) {
        if(desName==sock->getName()){
            emit sendDataToSocket(sock,pdu);            //找到对应的socket
            break;
        }
    }
}


void MyTcpServer::deleteSocket(MyTcpSocket *tcpSocket)
{
    //移除实例
    if(tcpSocket){
        m_tcpSocketList.removeOne(tcpSocket);
        QThread* thread = tcpSocket->thread();
        tcpSocket->close();
        tcpSocket->deleteLater();
        tcpSocket=nullptr;
        if(thread){
            thread->quit();
            thread->wait();
            thread->deleteLater();
            thread=nullptr;
        }
        displayLog("@@系统@@ :客户端退出，已关闭并移除当前tcp实例");
    }
}

void MyTcpServer::displayLog(const QString &logTxt)
{
    TcpServer::getInstance().logRecording(logTxt);
}
